function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength)
	return false
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ 
		if (pressedkey.test(String.fromCharCode(e.which)))
			e.stopPropagation()
	}
}
function countlimit(maxlength,e,placeholder){
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder)){
	if (lengthleft<0)
		theform.value=theform.value.substring(0,maxlength)
		placeholderobj.innerHTML=lengthleft
	}
}
function displaylimit(thename, theid, thelimit){
	var ns6=document.getElementById&&!document.all
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text='<strong><span id="'+theform.toString()+'">'+thelimit+'</span></strong> caractères restants.'
	if (document.all||ns6)
		document.write(limit_text)
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId);
	var scroll = field.scrollTop;
	field.focus();
	
	
	if (window.ActiveXObject) {
		var textRange = document.selection.createRange();            
		var currentSelection = textRange.text;
	} else {
		var startSelection   = field.value.substring(0, field.selectionStart);
		var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
		var endSelection     = field.value.substring(field.selectionEnd);
	}
	
	if (tagType) {
		switch (tagType) {
			case "lien":
					endTag = "</lien>";
					if (currentSelection) {
							if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									var label = prompt("Quel est le nom du lien?") || "";
									startTag = "<lien url=\"" + currentSelection + "\">";
									currentSelection = label;
							} else {
									var URL = prompt("Quelle est l'url?");
									startTag = "<lien url=\"" + URL + "\">";
							}
					} else {
							var URL = prompt("Quelle est l'url ?") || "";
							var label = prompt("Quel est le libellé du lien ?") || "";
							startTag = "<lien url=\"" + URL + "\">";
							currentSelection = label;                     
					}
			break;
			case "image":
					
					if (currentSelection) {
							if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									startTag = "<image>";
									endTag = "</image>";
							} else {
									var URL = alert("Erreur avec la selection du lien de l'image!") || "";
									startTag = URL + currentSelection ;
									currentSelection = "";
									endTag = "";
							}
					} else {
							var URL = prompt("Quelle est l'adresse url de l'image?") || "";
							startTag = "<image>" + URL;
							endTag = "</image>";
					}
			break;
			case "citation":
					endTag = "</citation>";
					if (currentSelection) {
							if (currentSelection.length > 30) {
									var auteur = prompt("Quel est l'auteur de la citation?") || "";
									startTag = "<citation nom=\"" + auteur + "\">";
							} else {
									var citation = prompt("Quelle est la citation?") || "";
									startTag = "<citation nom=\"" + currentSelection + "\">";
									currentSelection = citation;
							}
					} else {
							var auteur = prompt("Quel est l'auteur de la citation?") || "";
							var citation = prompt("Quelle est la citation ?") || "";
							startTag = "<citation nom=\"" + auteur + "\">";
							currentSelection = citation;    
					}
			break;	
		}
	}
	
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart('character', -endTag.length-currentSelection.length);
		textRange.moveEnd('character', -endTag.length);
		textRange.select();  
	} else { // Ce n'est pas IE
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}  
	
	field.scrollTop = scroll;   
}


function preview(textareaId, previewDiv) {
	var field = textareaId.value;
	
		var smiliesName = new Array(':colere:', ':diable:', ':ange:', ':ninja:', '&gt;_&lt;', ':pirate:', ':zorro:', ':honte:', ':soleil:', ':\'\\(', ':waw:', ':\\)', ':D', ';\\)', ':p', ':lol:', ':euh:', ':\\(', ':o', ':colere2:', 'o_O', '\\^\\^', ':\\-°', ':chinois:', ':charte:', ':bigrin:', ':batman:', ':attention:', ':ange:', ':arf:', ':clown:', ':confiant:', ':content:', ':cool:', ':cool2:', ':cooool:', ':cowboy:', ':decu:', ':diable:', ':dontcare:', ':epee20centmodif:', ':erk:', ':fillette:', ':fleched:', ':fouetbase:', ':gni:', ':grr:', ':grrr:', ':grucho:', ':gun:', ':hehe:', ':hihi:', ':hihi2:', ':hiver:', ':hum:', ':idee:', ':infirmiere:', ':langue:', ':love:', ':malade:', ':malade2:', ':mariee:', ':marin:', ':marrant:', ':mdr:', ':mort:', ':mouais:', ':myope:', ':niark:', ':ninja:', ':nooo:', ':nuage:', ':o:', ':Oo:', ':ouin:', ':oups:', ':penseur:', ':pfff:', ':pirate:', ':punk:', ':punk2:', ':ring:', ':robot:', ':robotange:', ':roi:', ':roi2:', ':rouge:', ':salut:', ':seventiz:', ':sick:', ':siffle:', ':slajou:', ':smile:', ':smiley29:', ':snif:', ':sniff:', ':soleil:', ':taureau:', ':tongue2:', ':tusors:', ':unsure2:', ':vengeance:', ':winks:',
':wouah:', ':x:', ':yeah:', ':yeah_baby:', ':yeepee:', ':yurk:', ':zibou:', ':zicgood:', ':zazoo:', ':zik:', ':zik2:', ':zik3:', ':zik4:', ':naaan:', ':3694:', ':bhouhou:', ':youpi:', ':3653:', ':saber:', ':fouet_2:', ':neutre_final:', ':deterrage:', ':dejarefuse:', ':bienjoue:', ':boulay:', ':jeplaidenoncoupable:', ':syn:', ':freeman:', ':euheh:', ':dort3pl:', ':nerd:', ':7966:', ':bluestorm1:', ':bluestorm2:', ':fouetaie:', ':enchaine:', ':narguile:', ':langue_002:', ':venerer:', ':love_002:', ':cut3647nerfs:', ':3630:', ':bibine:', ':3781:', ':3894:', ':3895:', ':3897:', ':4439:', ':4440:', ':3523:', ':3524:', ':3698:', ':15833:', ':15834:', ':15835:', ':15836:', ':15837:', ':15838:', ':15839:', ':15840:', ':15857:', ':15858:', ':15832:', ':marre0eb:', ':0:', ':1:', ':2:', ':3:', ':4:', ':5:', ':6:', ':7:', ':8:', ':9:', ':10:', ':papy:', ':chinois:', ':charte:', ':bigrin:', ':batman:', ':attention:', ':ange:', ':arf:', ':clown:', ':confiant:', ':content:', ':cool:', ':cool2:', ':cooool:', ':cowboy:', ':decu:', ':diable:', ':dontcare:', ':epee20centmodif:', ':erk:', ':fillette:', ':fleched:', ':gni:', ':grr:', ':grrr:', ':grucho:', ':gun:', ':hehe:', ':hihi:', ':hihi2:', ':hiver:', ':hum:', ':idee:', ':infirmiere:', ':langue:', ':love:', ':malade:', ':malade2:', ':mariee:', ':marin:', ':marrant:', ':mort:', ':mouais:', ':myope:', ':niark:', ':ninja:', ':nooo:', ':nuage:', ':o:', ':Oo:', ':ouin:', ':oups:', ':penseur:', ':pfff:', ':pirate:', ':punk:', ':punk2:', ':ring:', ':robot:', ':robotange:', ':roi:', ':roi2:', ':rouge:', ':salut:', ':seventiz:', ':sick:', ':siffle:', ':slajou:', ':smile:', ':smiley29:', ':snif:', ':sniff:', ':soleil:', ':taureau:', ':tongue2:', ':unsure2:', ':vengeance:', ':winks:');
		var smiliesUrl  = new Array('angry.gif', 'diable.png', 'ange.png', 'ninja.png', 'pinch.png', 'pirate.png', 'zorro.png', 'rouge.png', 'soleil.png', 'pleure.png', 'waw.png', 'smile.png', 'heureux.png', 'clin.png', 'langue.png', 'rire.gif', 'unsure.gif', 'triste.png', 'huh.png', 'mechant.png', 'blink.gif', 'hihi.png', 'siffle.png', 'chinois.gif', 'charte.gif', 'bigrin.gif', 'batman.gif', 'attention.gif', 'ange.gif', 'arf.gif', 'clown.gif', 'confiant.gif', 'content.gif', 'cool.gif', 'cool2.gif', 'cooool.gif', 'cowboy.gif', 'decu.gif', 'diable.gif', 'dontcare.gif', 'epee20centmodif.gif', 'erk.gif', 'fillette.gif', 'fleched.gif', 'fouetbase.gif', 'gni.gif', 'grr.gif', 'grrr.gif', 'grucho.gif', 'gun.gif', 'hehe.gif', 'hihi.gif', 'hihi2.gif', 'hiver.gif', 'hum.gif', 'idee.gif', 'infirmiere.gif', 'langue.gif', 'love.gif', 'malade.gif', 'malade2.gif', 'mariee.gif', 'marin.gif', 'marrant.gif', 'mdr.gif', 'mort.gif', 'mouais.gif', 'myope.gif', 'niark.gif', 'ninja.gif', 'nooo.gif', 'nuage.gif', 'o.gif', 'Oo.gif', 'ouin.gif', 'oups.gif', 'penseur.gif', 'pfff.gif', 'pirate.gif', 'punk.gif', 'punk2.gif', 'ring.gif', 'robot.gif', 'robotange.gif', 'roi.gif', 'roi2.gif', 'rouge.gif', 'salut.gif', 'seventiz.gif', 'sick.gif', 'siffle.gif', 'slajou.gif', 'smile.gif', 'smiley29.gif', 'snif.gif', 'sniff.gif', 'soleil.gif', 'taureau.gif', 'tongue2.gif', 'tusors.gif', 'unsure2.gif', 'vengeance.gif', 'winks.gif', 'wouah.gif', 'x.gif', 'yeah.gif', 'yeah_baby.gif', 'yeepee.gif', 'yurk.gif', 'zibou.gif', 'zicgood.gif', 'zazoo.gif', 'zik.gif', 'zik2.gif', 'zik3.gif', 'zik4.gif', '.gif', '.gif', 'naaan.gif', '3694.gif', 'bhouhou.gif', 'youpi.gif', '3653.gif', 'saber.gif', 'fouet_2.gif', 'neutre_final.gif', 'deterrage.gif', 'dejarefuse.gif', 'bienjoue.gif', 'boulay.gif', 'jeplaidenoncoupable.gif', 'syn.gif', 'freeman.gif', 'euheh.gif', 'dort3pl.gif', 'nerd.gif', '7966.gif', 'bluestorm1.gif', 'bluestorm2.gif', 'fouetaie.gif', 'enchaine.gif', 'narguile.gif', 'langue_002.gif', 'venerer.gif', 'love_002.gif', 'cut3647nerfs.gif', '3630.png', 'bibine.png', '3781.gif', '3894.gif', '3895.gif', '3897.gif', '4439.gif', '4440.png', '3523.gif', '3524.gif', '3698.gif', '15833.gif', '15834.gif', '15835.gif', '15836.gif', '15837.gif', '15838.gif', '15839.gif', '15840.gif', '15857.gif', '15858.gif', '15832.gif', 'marre0eb.gif', '0.gif', '1.gif', '2.gif', '3.gif', '4.gif', '5.gif', '6.gif', '7.gif', '8.gif', '9.gif', '10.gif', 'papy.gif', 'chinois.gif', 'charte.gif', 'bigrin.gif', 'batman.gif', 'attention.gif', 'ange.gif', 'arf.gif', 'clown.gif', 'confiant.gif', 'content.gif', 'cool.gif', 'cool2.gif', 'cooool.gif', 'cowboy.gif', 'decu.gif', 'diable.gif', 'dontcare.gif', 'epee20centmodif.gif', 'erk.gif', 'fillette.gif', 'fleched.gif', 'gni.gif', 'grr.gif', 'grrr.gif', 'grucho.gif', 'gun.gif', 'hehe.gif', 'hihi.gif', 'hihi2.gif', 'hiver.gif', 'hum.gif', 'idee.gif', 'infirmiere.gif', 'langue.gif', 'love.gif', 'malade.gif', 'malade2.gif', 'mariee.gif', 'marin.gif', 'marrant.gif', 'mort.gif', 'mouais.gif', 'myope.gif', 'niark.gif', 'ninja.gif', 'nooo.gif', 'nuage.gif', 'o.gif', 'Oo.gif', 'ouin.gif', 'oups.gif', 'penseur.gif', 'pfff.gif', 'pirate.gif', 'punk.gif', 'punk2.gif', 'ring.gif', 'robot.gif', 'robotange.gif', 'roi.gif', 'roi2.gif', 'rouge.gif', 'salut.gif', 'seventiz.gif', 'sick.gif', 'siffle.gif', 'slajou.gif', 'smile.gif', 'smiley29.gif', 'snif.gif', 'sniff.gif', 'soleil.gif', 'taureau.gif', 'tongue2.gif', 'unsure2.gif', 'vengeance.gif', 'winks.gif');
		var smiliesPath = "./editeur/images/smilies/";
	
		field = field.replace(/&/g, '&amp;');
		field = field.replace(/</g, '&lt;').replace(/>/g, '&gt;');
		field = field.replace(/\n/g, '<br />').replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		
		field = field.replace(/&lt;gras&gt;([\s\S]*?)&lt;\/gras&gt;/g, '<strong>$1</strong>');
		field = field.replace(/&lt;italique&gt;([\s\S]*?)&lt;\/italique&gt;/g, '<i>$1</i>');
		field = field.replace(/&lt;lien&gt;([\s\S]*?)&lt;\/lien&gt;/g, '<a href="$1">$1</a>');
		field = field.replace(/&lt;souligne&gt;([\s\S]*?)&lt;\/souligne&gt;/g, '<u>$1</u>');
		field = field.replace(/&lt;barre&gt;([\s\S]*?)&lt;\/barre&gt;/g, '<s>$1</s>');
		field = field.replace(/&lt;lien url="([\s\S]*?)"&gt;([\s\S]*?)&lt;\/lien&gt;/g, '<a href="$1" title="$2">$2</a>');
		field = field.replace(/&lt;centrer&gt;([\s\S]*?)&lt;\/centrer&gt;/g, '<div align="center">$1</div>');
		field = field.replace(/&lt;justifier&gt;([\s\S]*?)&lt;\/justifier&gt;/g, '<div align="justify">$1</div>');
		field = field.replace(/&lt;droit&gt;([\s\S]*?)&lt;\/droit&gt;/g, '<div align="right">$1</div>');
		field = field.replace(/&lt;gauche&gt;([\s\S]*?)&lt;\/gauche&gt;/g, '<div align="left">$1</div>');
		field = field.replace(/&lt;image&gt;([\s\S]*?)&lt;\/image&gt;/g, '<img src="$1" alt="Image" />');
		field = field.replace(/&lt;citation nom=\"(.*?)\"&gt;([\s\S]*?)&lt;\/citation&gt;/g, '<br /><span class="citation_titre">$1 :</span><div class="citation">$2</div>');
		field = field.replace(/&lt;information&gt;([\s\S]*?)&lt;\/information&gt;/g, '<div class="information"><img src="./editeur/images/boutons/info_g.png" alt="Image"> $1</div>');
		field = field.replace(/&lt;erreur&gt;([\s\S]*?)&lt;\/erreur&gt;/g, '<div class="erreur"><img src="./editeur/images/boutons/erreur_g.png" alt="Image"> $1</div>');
		field = field.replace(/&lt;attention&gt;([\s\S]*?)&lt;\/attention&gt;/g, '<div class="attention"><img src="./editeur/images/boutons/attention_g.png" alt="Image"> $1</div>');
		field = field.replace(/&lt;question&gt;([\s\S]*?)&lt;\/question&gt;/g, '<div class="question"><img src="./editeur/images/boutons/question_g.png" alt="Image"> $1</div>');
		field = field.replace(/&lt;taille valeur=\"(.*?)\"&gt;([\s\S]*?)&lt;\/taille&gt;/g, '<span class="$1">$2</span>');
		field = field.replace(/&lt;couleur nom=\"(.*?)\"&gt;([\s\S]*?)&lt;\/couleur&gt;/g, '<span class="$1">$2</span>');
		field = field.replace(/&lt;police nom=\"(.*?)\"&gt;([\s\S]*?)&lt;\/police&gt;/g, '<span class="$1">$2</span>');

		for (var i=0, c=smiliesName.length; i<c; i++) {
			field = field.replace(new RegExp( smiliesName[i] , "g"), "<img src='" + smiliesPath + smiliesUrl[i] + "' alt='" + smiliesUrl[i] + "' />");
		}
		
		document.getElementById(previewDiv).innerHTML = field;

	
	var DivID = document.getElementById("previewDiv");
	if (document.getElementById('previsualisation').checked && field) {
		DivID.style.display = "block";
	}
	else{
		DivID.style.display = "none";
	}
}

function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest();
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

function SmiliesEnPlus(URL,TextareaId) {
	window.open(URL,"PopUp","width=250, height=250, top=150, left=100, scrollbars=1, resizable=1");
	window.TextareaId = TextareaId;
}

function ModeEmploi(URL) {
	window.open(URL,"PopUp","width=500, height=500, top=150, left=100, scrollbars=1, resizable=1");
}

function MenuTaille()
{
	var menu_taille = document.getElementById("menu_taille");
	if(menu_taille.style.display == "none"){
		menu_taille.style.display = "block";
	}
	else{
		menu_taille.style.display = "none";
	}
}

function MenuBaliseSpeciale()
{
	var menu_balise = document.getElementById("menu_balises_speciales");
	if(menu_balise.style.display == "none"){
		menu_balise.style.display = "block";
	}
	else{
		menu_balise.style.display = "none";
	}
}

function MenuCouleur()
{
	var menu_couleur = document.getElementById("menu_couleur");
	if(menu_couleur.style.display == "none"){
		menu_couleur.style.display = "block";
	}
	else{
		menu_couleur.style.display = "none";
	}
}

function MenuPosition()
{
	var menu_couleur = document.getElementById("menu_position");
	if(menu_couleur.style.display == "none"){
		menu_couleur.style.display = "block";
	}
	else{
		menu_couleur.style.display = "none";
	}
}

function MenuPolice()
{
	var menu_couleur = document.getElementById("menu_police");
	if(menu_couleur.style.display == "none"){
		menu_couleur.style.display = "block";
	}
	else{
		menu_couleur.style.display = "none";
	}
}
